package edu.zut.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@WebServlet(name = "PropertiesServlet",urlPatterns = "/properties")
public class PropertiesServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        //加载资源文件
        InputStream inputStream = servletContext.getResourceAsStream("db.properties");
        //Properties：资源文件对象
        Properties properties = new Properties();
        //根据输入流获取其信息并加载到properties对象中
        properties.load(inputStream);
        String name = properties.getProperty("jdbc.username");
        System.out.println("name = " + name);
        String psw = properties.getProperty("jdbc.password");
        System.out.println("psw = " + psw);
    }
}
